<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MFilesecController extends MApplicationComponent implements MIController
{
    private $create_handler = NULL;
    private $size = 0;
    private $version_id = 0;
    
    public function invoke ($uri = NULL)
    {
                                parent::init();
        $hash = @$_REQUEST['hash'];
                                if (empty($hash)) {
            throw new MFilesException(
            FXP::t(MConst::PARAMS_ERROR . "Missing parameter 'hash'."), 
            MConst::HTTP_CODE_400);
        }
        
                                $url_manager = new MUrlManager();
        $path = $url_manager->parsePathFromUrl($uri);
        $root = $url_manager->parseRootFromUrl($uri);
        if ($path == false || $root == false) {
            throw new MFilesException(FXP::t(MConst::PATH_ERROR), MConst::HTTP_CODE_411);;
        }
        
        $path        = "/" . $path;
        $path_info   = MUtils::pathinfo_utf($path);
        $file_name   = $path_info["basename"];
        $parent_path = $path_info["dirname"];
                $this->handleCheckFileVersion($hash);
        
        $this->create_handler = MFilesCommon::initMFilesCommon();
        $this->create_handler->parent_path    = MUtils::convertStandardPath($parent_path);;
        $this->create_handler->file_name      = $file_name;
        $this->create_handler->root           = $root;
        $this->create_handler->path           = MUtils::convertStandardPath($path);
        $this->create_handler->type           = MUtils::mime_content_type($file_name);
        $this->create_handler->size           = $this->size;
        $this->create_handler->file_hash      = $hash;
        $this->create_handler->version_id     = $this->version_id;
        
                                $this->create_handler->SaveFileDetails();
        $this->create_handler->buildResult();
        
    }
    
    
    private function handleAssign() {
        header("HTTP/1.1 303 See Other");
        echo 'Upload File.';
        exit(0);
    }
    
    private function handleCheckFileVersion ($hash)
    {
        $file_version = MFileVersions::queryFileVersionBySignatrue($hash);
        if ($file_version == false && empty($file_version)) {
            $this->handleAssign();
        }
        
                $store_path = DOCUMENT_ROOT_BLOCK . MUtils::getPathBySplitStr ( $hash );
        if (file_exists ( $store_path ) == false) {
            $this->handleAssign();
        }
        
        $this->version_id = $file_version[0]['id'];
        $this->size       = $file_version[0]['file_size'];
    }
}